package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.domain.FileAttachment;
import com.ruoyi.business.domain.bo.FileAttachmentBo;
import com.ruoyi.business.domain.vo.FileAttachmentVo;
import com.ruoyi.business.mapper.FileAttachmentMapper;
import com.ruoyi.business.service.IFileAttachmentService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringExtUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 档案影像Service业务层处理
 *
 * @author xxyd
 * @date 2024-11-27
 */
@RequiredArgsConstructor
@Service
public class FileAttachmentServiceImpl implements IFileAttachmentService {

    private final FileAttachmentMapper baseMapper;

    /**
     * 查询档案影像
     */
    @Override
    public FileAttachmentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询档案影像列表
     */
    @Override
    public TableDataInfo<FileAttachmentVo> queryPageList(FileAttachmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FileAttachment> lqw = buildQueryWrapper(bo);
        Page<FileAttachmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询档案影像列表
     */
    @Override
    public List<FileAttachmentVo> queryList(FileAttachmentBo bo) {
        LambdaQueryWrapper<FileAttachment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FileAttachment> buildQueryWrapper(FileAttachmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FileAttachment> lqw = Wrappers.lambdaQuery();
        lqw.like(StringExtUtils.isNotBlank(bo.getName()), FileAttachment::getName, bo.getName());
        lqw.eq(bo.getPersonFileId() != null, FileAttachment::getPersonFileId, bo.getPersonFileId());
        lqw.eq(StringExtUtils.isNotBlank(bo.getArchivalImagery()), FileAttachment::getArchivalImagery, bo.getArchivalImagery());
        lqw.eq(bo.getOrderNum() != null, FileAttachment::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getStatus() != null, FileAttachment::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增档案影像
     */
    @Override
    public Boolean insertByBo(FileAttachmentBo bo) {
        FileAttachment add = BeanUtil.toBean(bo, FileAttachment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改档案影像
     */
    @Override
    public Boolean updateByBo(FileAttachmentBo bo) {
        FileAttachment update = BeanUtil.toBean(bo, FileAttachment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FileAttachment entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除档案影像
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
